<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商品搜索">
          <el-input
            v-model="formInline.skuName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <el-button
        style="margin-bottom: 10px"
        type="success"
        size="small"
      >导入数据</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="商品名称" prop="skuName" />
        <el-table-column label="商品图片" prop="skuImage">
          <template #default="{ row }">
            <div class="imgRow">
              <img :src="row.skuImage" class="vm-type-image">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="品牌" prop="brandName" />
        <el-table-column
          label="规格"
          prop="unit"
        />
        <el-table-column
          label="商品价格"
          prop="price"
        >
          <template #default="{ row }">
            {{ row.price/100 }}
          </template>
        </el-table-column>
        <el-table-column label="商品类型" prop="skuClass.className" />
        <el-table-column
          label="创建日期"
          prop="createTime"
          width="170"
        >
          <template #default="{ row }">
            {{ new Date(row.createTime).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row v-if="totalPage!==1" type="flex" justify="space-between" align="middle">
        <el-col :span="19">
          <p class="paginationHeader">
            {{
              `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页`
            }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!--新增商品类型-->
    <el-dialog
      :title="title"
      :visible="dialogSku"
      width="45%"
      @open="opneDialog"
      @close="closeDialog"
    >
      <el-form
        ref="userRef"
        label-width="120px"
        :model="userForm"
        :rules="userFormRules"
      >
        <el-form-item label="商品名称" prop="skuName">
          <el-input
            v-model="userForm.skuName"
            type="text"
            placeholder="请输入"
            maxlength="15"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brandName">
          <el-input
            v-model="userForm.brandName"
            type="text"
            placeholder="请输入"
            maxlength="10"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="商品价格(元)" prop="price">
          <el-input-number
            v-model="userForm.price"
            style="width: 80%"
            controls-position="right"
            :min="0"
            :max="100"
            :step="0.5"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="商品类型" prop="classId">
          <el-select
            v-model="userForm.classId"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in classList"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规格" prop="unit">
          <el-input
            v-model="userForm.unit"
            type="text"
            placeholder="请输入"
            maxlength="10"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="商品图片" prop="skuImage">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :http-request="upload"
            :before-upload="beforeAvatarUpload"
          >
            <!-- 上传后展示的图片 -->
            <img v-if="userForm.skuImage" :src="userForm.skuImage" class="avatar">
            <!-- 默认的加号图片 -->
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <span> {{ `支持扩展名:jpg、png,文件不得大于100kb` }}</span>
        </el-form-item></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel" @click="closeDialog">取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="userConfig"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { skuList, skuClassList, Addsku, Editsku } from '@/api/sku'
export default {
  data() {
    return {
      formInline: { skuName: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      pageOption: {
        pageIndex: 1,
        pageSize: 100000
      },
      totalCount: 0,
      totalPage: 0,
      // 新增商品弹出框-------------------------------------------------
      classList: [], // 商品类型
      dialogSku: false,
      userForm: {
        skuName: '', // 商品名称
        brandName: '', // 品牌
        price: '', // 商品价格(元)
        classId: '', // 商品类型
        unit: '', // 规格
        skuImage: '' // 商品图片
      },
      userFormRules: {
        skuName: [{ required: true, message: '请输入', trigger: 'blur' }],
        brandName: [{ required: true, message: '请输入', trigger: 'blur' }],
        price: [{ required: true, message: '请输入', trigger: 'change' }],
        classId: [{ required: true, message: '请输入', trigger: 'blur' }],
        unit: [{ required: true, message: '请输入', trigger: 'blur' }],
        skuImage: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  computed: {
    title() {
      return this.userForm.skuId ? '修改商品' : '新增商品'
    }
  },
  created() {
    this.skuList()
  },
  methods: {
    async skuList(skuName) {
      this.loading = true
      const { data } = await skuList({ ...this.page, ...skuName })
      this.loading = false
      this.list = data.currentPageRecords
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.skuList(this.formInline)
    },
    // 2.新增
    addForm() {
      this.dialogSku = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.skuList()
    },
    // 4.查看详情
    detailsFn(row) {
      this.userForm = row
      this.userForm.price = row.price / 100
      this.dialogSku = true
    },
    // ------------------------------------------------------------------------
    // 5.新增商品弹出层
    // 5.1.确定
    userConfig() {
      this.$refs.userRef.validate(async(isOk) => {
        if (!isOk) return
        if (this.userForm.skuId) {
          await Editsku(this.userForm)
        } else {
          await Addsku(this.userForm)
        }

        this.skuList()
        this.dialogSku = false
      })
    },
    // 5.2.取消
    closeDialog() {
      this.userForm = {
        skuName: '', // 商品名称
        brandName: '', // 品牌
        price: '', // 商品价格(元)
        classId: '', // 商品类型
        unit: '', // 规格
        skuImage: '' // 商品图片
      }
      this.dialogSku = false
    },
    // 5.3.弹出层打开
    async  opneDialog() {
      const { data: { currentPageRecords }} = await skuClassList(this.pageOption)
      this.classList = currentPageRecords
    },
    // 5.4表单图片
    // 图片校验
    beforeAvatarUpload(file) {
      const fileList = ['image/jpeg', 'image/png']
      const isType = fileList.includes(file.type)
      const isLt100kb = file.size / 1024 < 100
      if (!isType) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt100kb) {
        this.$message.error('上传头像图片大小不能超过 100kB!')
      }
      return isType && isLt100kb
    },
    upload(file) {
      console.log(file)
      this.userForm.skuImage = `http://likede2-java.itheima.net/image/${file.file.name}`
    }
  }
}
</script>

<style lang="scss">
 .imgRow {
  .vm-type-image {
    display: block;
    background-color: pink;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    object-fit: cover;
  }
}
.el-dialog .el-input .el-input__inner {
  height: 40px !important;
}
</style>
